package com.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.beans.Car;
import com.beans.Plane;
import com.beans.Train;
import com.beans.User;
import com.mapper.CarMapper;
import com.mapper.PlaneMapper;
import com.mapper.PlaneOrderMapper;
import com.mapper.TrainMapper;
import com.mapper.UserMapper;

import ch.qos.logback.core.net.SyslogOutputStream;

@Controller
public class UserController {
	
	private String loginNumber;
	@Resource
	private UserMapper usermapper;
	@Resource
	private PlaneMapper planemapper;
	@Resource
	private CarMapper carmapper;
	@Resource
	private TrainMapper trainmapper;
	@Resource
	private PlaneOrderMapper planeordermapper;

//	@GetMapping("userall.html")
//	public String userlist1(ModelMap m) {
//     List<User> list = usermapper.selectAll();
//    	m.put("list", list);	
//    	m.addAttribute("msg","共有 " +selectsum()+" 条数据");
//		return "userall.html"; 
//	}
//int selectsum() {
//   	 return usermapper.selectuserSum();
//    }
//
//@PostMapping("/adduser")  
//public String addUser1(User user,ModelMap m) {
//	usermapper.addUser(user);
//	 m.put("msg","发布成功");
//	 return "useradd";
//}
//@GetMapping("/delete1/{id}")
//public String delete1(@PathVariable int id,ModelMap m) {
//  usermapper.del_user(id);
//	return userlist(m);
//	
//}
//@GetMapping("/modification1/{id}")    
//public String modification1(@PathVariable int id, Model model){
//    User user = usermapper.getUserById(id);
//    model.addAttribute("user", user);
//    return "usermodification";
//} 
////真正的进行修改
//@PostMapping("/update1")
//	public String update(User user,ModelMap m) {
//	  usermapper.update_user(user);
//		m.put("msg", "用户信息更新成功");
//		return "usermodification"; 
//}
//@PostMapping("/search1")
//public String search(int id,ModelMap m) {
//	User user= usermapper.getUserById(id);
//	m.put("user", user);
//	return "usersearch";
//}


@GetMapping("top.html")
public String index(ModelMap m) {
	m.put("loginNumber", loginNumber);
	return "top";
}


@GetMapping("index.html")
public String index3(ModelMap m) {
	m.put("loginNumber", loginNumber);
	return "index";
}

@RequestMapping("/login") 
public String loginuser(String number,String password,ModelMap m){
	//进行数据库访问
	User user=usermapper.loginuser(number, password);
	if (user!=null) {
		loginNumber=number;
		index(m);
		index3(m);
	
		return "main";  
	}
	else {
		m.put("msg", "用户名或密码错误,登录失败");
		return "login";
	}	
}

@GetMapping("/detailplaneorder/{id}")    
public String deailplaneorder(@PathVariable int id, Model model,ModelMap m){	 
    Plane plane = planemapper.getPlaneById(id);
    m.put("loginNumber", loginNumber);
    model.addAttribute("plane", plane);
	return "plane-details";
} 

@GetMapping("/detailtrainorder/{id}")    
public String deailtrainorder(@PathVariable int id, Model model,ModelMap m){	 
    Train train = trainmapper.getTrainById(id);
    m.put("loginNumber", loginNumber);
    model.addAttribute("train", train);
	return "train-details";
} 

@GetMapping("/detailcarorder/{id}")    
public String deailcarorder(@PathVariable int id, Model model,ModelMap m){	 
    Car car = carmapper.getCarById(id);
    m.put("loginNumber", loginNumber);
    model.addAttribute("car", car);
	return "car-details";
}
@GetMapping("userlist-all.html")
public String userlist(ModelMap m) {
 List<User> userlist = usermapper.selectuserAll();
	m.put("userlist", userlist);	
	m.addAttribute("msg","共有 " +selectusersum()+" 条数据");
	return "userlist-all.html"; 
}
int selectusersum() {
	 return usermapper.selectuserSum();
}

@PostMapping("/adduser")  
public String addUser(User user,ModelMap m) {
usermapper.addUser(user);
 m.put("msg","发布成功");
 return userlist(m);
}
@GetMapping("/deleteuser/{id}")
public String delete(@PathVariable int id,ModelMap m) {
usermapper.del_user(id);
return userlist(m);

}
@GetMapping("/modification/{id}")    
public String modification(@PathVariable int id, Model model){
User user = usermapper.getUserById(id);
model.addAttribute("user", user);
return "user-update.html";
} 
//真正的进行修改
@PostMapping("/updateuser")
public String updateuser(User user,ModelMap m) {
  usermapper.update_user(user);
	m.put("msg", "用户信息更新成功");
	return userlist(m); 
}
@PostMapping("/searchuser")
public String searchuser(String name,ModelMap m) {
User user= usermapper.getUserByname(name);
m.put("user", user);
return "user-add.html";
}
}
